package com.daily.pump.dialog;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.daily.pump.R;
import com.daily.pump.app.App;
import com.daily.pump.model.Device;
import com.daily.pump.widget.CompressProgress;


/**
 * Desc:气压-长按设置气压弹框
 * <p>
 * Author: [李豫]
 * Date: 2018-08-02
 * Copyright: Copyright (c) 2010-2018
 * Updater:
 * Update Time:
 * Update Comments:
 */
public class CompressSettingDialog extends DialogFragment implements View.OnClickListener {

    public static final int TYPE_SOFT = 0;
    public static final int TYPE_MEDIUM = 1;
    public static final int TYPE_FIRM = 2;
    public static final int TYPE_SETTING_VALUE = 3;
    private static final String KEY_TYPE = "keyType";
    private static final String KEY_VALUE = "keyValue";

    private int mType;
    private CompressProgress compressPb;
    private EditText compressEt;
    private TextView compressTv;
    private int mProgress;

    public static CompressSettingDialog newInstance(int type, int value) {
        CompressSettingDialog commentDeleteDialog = new CompressSettingDialog();
        Bundle bundle = new Bundle();
        bundle.putInt(KEY_TYPE, type);
        bundle.putInt(KEY_VALUE, value);
        commentDeleteDialog.setArguments(bundle);
        return commentDeleteDialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCancelable(true);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.customDialog);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDialog().setCanceledOnTouchOutside(true);
        Window window = getDialog().getWindow();
        window.setGravity(Gravity.CENTER);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View dialogView = inflater.inflate(R.layout.dialog_compress_setting, container, false);
        initData(dialogView);
        return dialogView;
    }

    private void initData(View dialogView) {
        Bundle arguments = getArguments();
        if (arguments != null) {
            mType = arguments.getInt(KEY_TYPE);
            mProgress = arguments.getInt(KEY_VALUE);
        }
        compressTv = dialogView.findViewById(R.id.compress_tv);
        compressPb = dialogView.findViewById(R.id.compress_pb);
        compressPb.setCompressViewListener(new CompressProgress.CompressViewListener() {
            @Override
            public void getCompressValue(float percent) {

            }

            @Override
            public void onAdjustCompress(float percent) {
                mProgress = (int) (percent * 100);
                String valueStr = String.valueOf(mProgress);
                compressTv.setText(valueStr);
                compressEt.setText(valueStr);
            }
        });
        compressEt = dialogView.findViewById(R.id.et_compress);
        compressEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String valueStr = s.toString();
                int value = 0;
                if (!TextUtils.isEmpty(valueStr)) {
                    value = Integer.parseInt(valueStr);
                }
                if (value > 100) {
                    value = 100;
                    String str = String.valueOf(value);
                    compressEt.setText(str);
                    compressEt.setSelection(str.length());
                }
                mProgress = value;
                compressPb.setProgress(value / 100.0f);
                compressTv.setText(String.valueOf(value));
            }
        });
        dialogView.findViewById(R.id.save_tv).setOnClickListener(this);
        dialogView.findViewById(R.id.cancel_tv).setOnClickListener(this);

        compressPb.setProgress(mProgress / 100.0f);
        String valueStr = String.valueOf(mProgress);
        compressTv.setText(valueStr);
        compressEt.setText(valueStr);
        compressEt.setSelection(valueStr.length());
    }

    public void show(FragmentManager manager) {
        if (!isShowing()) {
            show(manager, CompressSettingDialog.class.getSimpleName());
        }
    }

    public boolean isShowing() {
        if (getDialog() != null && getDialog().isShowing()) {
            return true;
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        if (viewId == R.id.cancel_tv) {
            dismiss();
        } else if (viewId == R.id.save_tv) {
            saveCompress();
            dismiss();
        }
    }

    private void saveCompress() {
        Device loginDevice = App.getInstance().getLoginDevice();
        if (loginDevice != null) {
            if (mType == TYPE_SOFT) {
                loginDevice.softValue = mProgress;
            } else if (mType == TYPE_MEDIUM) {
                loginDevice.mediumValue = mProgress;
            } else if (mType == TYPE_FIRM) {
                loginDevice.firmValue = mProgress;
            }
            App.getInstance().setLoginDevice(loginDevice);
            if (mSettingListener != null) {
                mSettingListener.onSetting(mType, mProgress);
            }
        }
    }

    public CompressSettingListener mSettingListener;

    public void setCompressSettingListener(CompressSettingListener listener) {
        this.mSettingListener = listener;
    }

    public interface CompressSettingListener {
        void onSetting(int type, int progress);
    }
}

